package com.kky.pp.popup

import android.content.Context
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.kky.pp.common.Constant
import com.kky.pp.R
import com.kky.pp.entity.AgreementEntity
import com.kky.pp.entity.BuyMemberPageEntity
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.SDOkHttpResoutCallBack
import com.custom.base.util.IntentUtil
import io.rong.imkit.activity.RongWebviewActivity
import razerdp.basepopup.BasePopupWindow

class MemberUnitaryBuyDialog(
    var ctx: Context,
    var productCategory: Int,
    var listener: MemberUnitaryBuyListener
) : BasePopupWindow(ctx) {
    init {
        setContentView(R.layout.popup_buy_member_unitary)
        initView()
    }

    private fun initView() {
        val memberDialog = findViewById<ConstraintLayout>(R.id.conDialogBuyQuickChatContainer)


        val dialogPurchaseNow = findViewById<TextView>(R.id.txtDialogBuyQuickChatNow)
        val dialogCancel = findViewById<ImageView>(R.id.imgViewDialogBuyQuickChatCLose)
        val imgTop = findViewById<ImageView>(R.id.imgViewDialogBuyQuickChatTopLogo)
        val itemTitle = findViewById<TextView>(R.id.txtDialogBuyQuickChatTitle)
        val itemContent = findViewById<TextView>(R.id.txtDialogBuyQuickChatContent)

        val quarterContainer = findViewById<ConstraintLayout>(R.id.conDialogBuyQuickChatTimesContainer)
        val itemQuarterTitle = findViewById<TextView>(R.id.txtDialogBuyQuickChatTimesTitle)
        val itemQuarterNumber = findViewById<TextView>(R.id.txtDialogBuyQuickChatTimesNumber)
        val itemQuarterName = findViewById<TextView>(R.id.txtDialogBuyQuickChatTimesName)
        val itemQuarterDiscountedPrice = findViewById<TextView>(R.id.txtDialogBuyQuickChatTimesPrice)

        val monthlyContainer = findViewById<ConstraintLayout>(R.id.conDialogBuyQuickChatMonthlyContainer)
        val itemMonthlyTitle = findViewById<TextView>(R.id.txtDialogBuyQuickChatMonthlyTitle)
        val itemMonthlyNumber = findViewById<TextView>(R.id.txtDialogBuyQuickChatMonthlyNumber)
        val itemMonthlyName = findViewById<TextView>(R.id.txtDialogBuyQuickChatMonthlyName)
        val itemMonthlyDiscountedPrice = findViewById<TextView>(R.id.txtDialogBuyQuickChatMonthlyPrice)

        val weeksContainer = findViewById<ConstraintLayout>(R.id.conDialogBuyQuickChatWeeksContainer)
        val itemWeeksNumber = findViewById<TextView>(R.id.txtDialogBuyQuickChatWeeksNumber)
        val itemWeeksName = findViewById<TextView>(R.id.txtDialogBuyQuickChatWeeksName)
        val itemWeeksDiscountedPrice = findViewById<TextView>(R.id.txtDialogBuyQuickChatWeeksPrice)

        val txtBuySingleTips = findViewById<TextView>(R.id.txtDialogBuyQuickChatTips)
        var model: BuyMemberPageEntity.ProductExt? = null


        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(Constant.user_member_product_category_url)
                requestBody.add("productCategory", productCategory)
            }

        }, object : SDOkHttpResoutCallBack<BuyMemberPageEntity>() {
            override fun onSuccess(entity: BuyMemberPageEntity) {
                itemTitle.text = entity.data.subscriptions[0].tip
                itemContent.text = entity.data.subscriptions[0].content
                when (productCategory) {
                    2 -> {
                        setViewInfo(
                            imgTop,
                            memberDialog,
                            dialogPurchaseNow,
                            quarterContainer,
                            entity,
                            monthlyContainer,
                            weeksContainer,
                            R.mipmap.icon_popup_flash_chat,
                            R.mipmap.icon_member_unitary_fc_bg,
                            R.drawable.shape_e46ed8_radius_25,
                            R.drawable.shape_member_fc_select_bg,
                            R.drawable.shape_a94e9b_radius_10,
                        )
                    }

                    3 -> {
                        setViewInfo(
                            imgTop,
                            memberDialog,
                            dialogPurchaseNow,
                            quarterContainer,
                            entity,
                            monthlyContainer,
                            weeksContainer,
                            R.mipmap.icon_popup_private_photos,
                            R.mipmap.icon_member_unitary_pp_bg,
                            R.drawable.shape_ce4de3_radius_25,
                            R.drawable.shape_member_pp_select_bg,
                            R.drawable.shape_9754a7_radius_10,
                        )
                    }

                    4 -> {
                        setViewInfo(
                            imgTop,
                            memberDialog,
                            dialogPurchaseNow,
                            quarterContainer,
                            entity,
                            monthlyContainer,
                            weeksContainer,
                            R.mipmap.icon_popup_private_videos,
                            R.mipmap.icon_member_unitary_pv_bg,
                            R.drawable.shape_ff4768_raiuds_25,
                            R.drawable.shape_member_pv_select_bg,
                            R.drawable.shape_ad5268_radius_10,
                        )

                    }
                }
                for (i in 0 until entity.data.productDescriptions.size) {
                    if (entity.data.productDescriptions[i].check == true) {
                        model = entity.data.productDescriptions[i]
                    }
                }

                itemQuarterTitle.text = entity.data.productDescriptions[2].tip
                itemQuarterNumber.text = entity.data.productDescriptions[2].benefitNum
                itemQuarterName.text = entity.data.productDescriptions[2].benefitUnit
                itemQuarterDiscountedPrice.text = "$"+entity.data.productDescriptions[2].price

                itemMonthlyTitle.text = entity.data.productDescriptions[1].tip
                itemMonthlyNumber.text = entity.data.productDescriptions[1].benefitNum
                itemMonthlyName.text = entity.data.productDescriptions[1].benefitUnit
                itemMonthlyDiscountedPrice.text ="$"+ entity.data.productDescriptions[1].price

                itemWeeksNumber.text = entity.data.productDescriptions[0].benefitNum
                itemWeeksName.text = entity.data.productDescriptions[0].benefitUnit
                itemWeeksDiscountedPrice.text = "$"+entity.data.productDescriptions[0].price



                quarterContainer.setOnClickListener {
                    entity.data.productDescriptions[2].check = true
                    model = entity.data.productDescriptions[2]
                    entity.data.productDescriptions[1].check = false
                    entity.data.productDescriptions[0].check = false

                    when (productCategory) {
                        2 -> {
                            setViewInfo(
                                imgTop,
                                memberDialog,
                                dialogPurchaseNow,
                                quarterContainer,
                                entity,
                                monthlyContainer,
                                weeksContainer,
                                R.mipmap.icon_popup_flash_chat,
                                R.mipmap.icon_member_unitary_fc_bg,
                                R.drawable.shape_e46ed8_radius_25,
                                R.drawable.shape_member_fc_select_bg,
                                R.drawable.shape_a94e9b_radius_10,
                            )
                        }

                        3 -> {
                            setViewInfo(
                                imgTop,
                                memberDialog,
                                dialogPurchaseNow,
                                quarterContainer,
                                entity,
                                monthlyContainer,
                                weeksContainer,
                                R.mipmap.icon_popup_private_photos,
                                R.mipmap.icon_member_unitary_pp_bg,
                                R.drawable.shape_ce4de3_radius_25,
                                R.drawable.shape_member_pp_select_bg,
                                R.drawable.shape_9754a7_radius_10,
                            )
                        }

                        4 -> {
                            setViewInfo(
                                imgTop,
                                memberDialog,
                                dialogPurchaseNow,
                                quarterContainer,
                                entity,
                                monthlyContainer,
                                weeksContainer,
                                R.mipmap.icon_popup_private_videos,
                                R.mipmap.icon_member_unitary_pv_bg,
                                R.drawable.shape_ff4768_raiuds_25,
                                R.drawable.shape_member_pv_select_bg,
                                R.drawable.shape_ad5268_radius_10,
                            )

                        }
                    }
                }

                monthlyContainer.setOnClickListener {
                    entity.data.productDescriptions[2].check = false
                    entity.data.productDescriptions[1].check = true
                    model = entity.data.productDescriptions[1]
                    entity.data.productDescriptions[0].check = false

                    when (productCategory) {
                        2 -> {
                            setViewInfo(
                                imgTop,
                                memberDialog,
                                dialogPurchaseNow,
                                quarterContainer,
                                entity,
                                monthlyContainer,
                                weeksContainer,
                                R.mipmap.icon_popup_flash_chat,
                                R.mipmap.icon_member_unitary_fc_bg,
                                R.drawable.shape_e46ed8_radius_25,
                                R.drawable.shape_member_fc_select_bg,
                                R.drawable.shape_a94e9b_radius_10,
                            )
                        }

                        3 -> {
                            setViewInfo(
                                imgTop,
                                memberDialog,
                                dialogPurchaseNow,
                                quarterContainer,
                                entity,
                                monthlyContainer,
                                weeksContainer,
                                R.mipmap.icon_popup_private_photos,
                                R.mipmap.icon_member_unitary_pp_bg,
                                R.drawable.shape_ce4de3_radius_25,
                                R.drawable.shape_member_pp_select_bg,
                                R.drawable.shape_9754a7_radius_10,
                            )
                        }

                        4 -> {
                            setViewInfo(
                                imgTop,
                                memberDialog,
                                dialogPurchaseNow,
                                quarterContainer,
                                entity,
                                monthlyContainer,
                                weeksContainer,
                                R.mipmap.icon_popup_private_videos,
                                R.mipmap.icon_member_unitary_pv_bg,
                                R.drawable.shape_ff4768_raiuds_25,
                                R.drawable.shape_member_pv_select_bg,
                                R.drawable.shape_ad5268_radius_10,
                            )

                        }
                    }


                }

                weeksContainer.setOnClickListener {
                    entity.data.productDescriptions[2].check = false
                    entity.data.productDescriptions[1].check = false
                    entity.data.productDescriptions[0].check = true
                    model = entity.data.productDescriptions[0]

                    when (productCategory) {
                        2 -> {
                            setViewInfo(
                                imgTop,
                                memberDialog,
                                dialogPurchaseNow,
                                quarterContainer,
                                entity,
                                monthlyContainer,
                                weeksContainer,
                                R.mipmap.icon_popup_flash_chat,
                                R.mipmap.icon_member_unitary_fc_bg,
                                R.drawable.shape_e46ed8_radius_25,
                                R.drawable.shape_member_fc_select_bg,
                                R.drawable.shape_a94e9b_radius_10,
                            )
                        }

                        3 -> {
                            setViewInfo(
                                imgTop,
                                memberDialog,
                                dialogPurchaseNow,
                                quarterContainer,
                                entity,
                                monthlyContainer,
                                weeksContainer,
                                R.mipmap.icon_popup_private_photos,
                                R.mipmap.icon_member_unitary_pp_bg,
                                R.drawable.shape_ce4de3_radius_25,
                                R.drawable.shape_member_pp_select_bg,
                                R.drawable.shape_9754a7_radius_10,
                            )
                        }

                        4 -> {
                            setViewInfo(
                                imgTop,
                                memberDialog,
                                dialogPurchaseNow,
                                quarterContainer,
                                entity,
                                monthlyContainer,
                                weeksContainer,
                                R.mipmap.icon_popup_private_videos,
                                R.mipmap.icon_member_unitary_pv_bg,
                                R.drawable.shape_ff4768_raiuds_25,
                                R.drawable.shape_member_pv_select_bg,
                                R.drawable.shape_ad5268_radius_10,
                            )

                        }
                    }

                }

                showPopupWindow()
            }
        })

        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(Constant.user_config_url)
                requestBody.add("code", 2)
            }
        }, object : SDOkHttpResoutCallBack<AgreementEntity>() {
            override fun onSuccess(entity: AgreementEntity) {
                val style = SpannableStringBuilder()
                style.append(ctx.getString(R.string.buy_member_unitary_tips))

                style.setSpan(object : ClickableSpan() {
                    override fun onClick(p0: View) {
                        val b = Bundle()
                        b.putString("url", entity.data.privacyPolicy)
                        b.putString(
                            "title",
                            ctx.resources.getString(R.string.privacy_policy)
                        )
                        IntentUtil.startActivity(RongWebviewActivity::class.java, b)
                    }

                    override fun updateDrawState(ds: TextPaint) {
                        ds.isUnderlineText = true
                        ds.clearShadowLayer()
                    }
                }, 32, 46, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                style.setSpan(object : ClickableSpan() {
                    override fun onClick(p0: View) {
                        val b = Bundle()
                        b.putString("url", entity.data.terms)
                        b.putString(
                            "title",
                            ctx.resources.getString(R.string.terms)
                        )
                        IntentUtil.startActivity(RongWebviewActivity::class.java, b)
                    }

                    override fun updateDrawState(ds: TextPaint) {
                        ds.isUnderlineText = true
                        ds.clearShadowLayer()
                    }
                }, 51, 56, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

                txtBuySingleTips.text = style
                txtBuySingleTips.movementMethod = LinkMovementMethod.getInstance()
            }
        })


        dialogPurchaseNow.setOnClickListener {
            model?.let { it1 -> listener.onListener(it1) }
            dismiss()
        }
        dialogCancel.setOnClickListener {
            dismiss()
        }


        setOutSideDismiss(true)
    }

    /**
     * 设置控件颜色和背景
     */
    private fun setViewInfo(
        imgTop: ImageView,
        memberDialog: ConstraintLayout,
        dialogPurchaseNow: TextView,
        quarterContainer: ConstraintLayout,
        entity: BuyMemberPageEntity,
        monthlyContainer: ConstraintLayout,
        weeksContainer: ConstraintLayout,
        topImg: Int,
        dialogBg: Int,
        dialogTitleBg: Int,
        selectBg: Int,
        unSelectBg: Int,
    ) {
        imgTop.setImageResource(topImg)
        memberDialog.setBackgroundResource(dialogBg)
        dialogPurchaseNow.setBackgroundResource(dialogTitleBg)
        quarterContainer.setBackgroundResource(if (entity.data.productDescriptions[2].check == true) selectBg else unSelectBg)
        monthlyContainer.setBackgroundResource(if (entity.data.productDescriptions[1].check == true) selectBg else unSelectBg)
        weeksContainer.setBackgroundResource(if (entity.data.productDescriptions[0].check == true) selectBg else unSelectBg)
    }


    interface MemberUnitaryBuyListener {
        fun onListener(bean: BuyMemberPageEntity.ProductExt)
    }

}